package com.ejuornal.action;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import com.ejuornal.bean.EditorChief;
import com.ejuornal.bean.User;
import com.ejuornal.bean.UserExt;
import com.ejuornal.service.EditorChiefService;
import com.ejuornal.service.MessageService;
import com.ejuornal.service.UserExtService;
import com.ejuornal.service.UserService;
import com.ejuornal.springutil.SpringUtil;
import com.ejuornal.util.UploadUtil;

public class EditorChiefAction extends BaseAction<EditorChief> {
	private static final long serialVersionUID = 1L;
	private EditorChiefService editorChiefService;
	private EditorChief editorChief;
	private MessageService messageService;
	private String value;
	private String editorChiefIdStr;
	private Map<String, Object> dataMap = new HashMap<String, Object>();
	
	private UserService userService;
    private User user;
    private UserExtService userExtService = null;
   	private UserExt userExt;
   	
   	private File businessImg;//营业执照 图片
	private String businessImgFileName;//上传文件名
	private String businessImgContentType;
	
	private File codeImg;//代码证
	private String codeImgFileName;//上传文件名
	private String codeImgContentType;
	/**
	 * 申请主编的jsp页面
	 * @return
	 */
   	public String addChiefCheckInfo(){
   		return SUCCESS;
   	}
	/**
	 * 申请主编
	 * @author syb
	 * @return
	 */
	public String add() {
		//将申请主编的信息添加到数据中
		editorChief.setBusinessImg(UploadUtil.upload(businessImg, businessImgFileName));
		editorChief.setCodeImg(UploadUtil.upload(codeImg, codeImgFileName));
		editorChief.setUserId(super.getUserIdBySession());
		editorChief.setStatus("1");	//未审核主编状态
		editorChiefService.add(editorChief);
		
		//将申请主编的公司名称添加到用户扩展表中
		UserExtService userExtService = (UserExtService)SpringUtil.getBean("userExtService");
		UserExt userExt = userExtService.findByUserId(super.getUserIdBySession());
		userExt.setCompany((String)super.getRequestParameter("company"));
		userExtService.update(userExt);
		
		return "success";
	}
	/*
	 * 根据主编状态查询主编列表gcf
	 */
	public String findByStatus(){
		String url="user/findChiefByStatus?value=" + value + "&";
		
		editorChiefService=(EditorChiefService) SpringUtil.getBean("editorChiefService");
		if("1".equals(value))
		{   //未审核
			pages = editorChiefService.findByStatus((page-1)*10, url, value);

			return SUCCESS;
		}
		else if("2".equals(value))
		{   //审核通过
			pages = editorChiefService.findByStatus((page-1)*10, url, value);

			return SUCCESS;
		}
		else if("3".equals(value))
		{   //审核未通过
			pages = editorChiefService.findByStatus((page-1)*10, url, value);
			
			return SUCCESS;
		}
		else 
		{   //异常
			return ERROR;
		}
	
	}
	public String findByStatus2(){
		String url="user/findChiefByStatus?value=" + value + "&";
		
		editorChiefService=(EditorChiefService) SpringUtil.getBean("editorChiefService");
		if("1".equals(value))
		{   //未审核
			pages = editorChiefService.findByStatus((page-1)*10, url, value);

			return SUCCESS;
		}
		else if("2".equals(value))
		{   //审核通过
			pages = editorChiefService.findByStatus((page-1)*10, url, value);

			return SUCCESS;
		}
		else if("3".equals(value))
		{   //审核未通过
			pages = editorChiefService.findByStatus((page-1)*10, url, value);
			
			return SUCCESS;
		}
		else 
		{   //异常
			return ERROR;
		}
	
	}
	/*
	 * 根据主编id查询主编数据
	 */
	public String findByChiefId(){
		int editorChiefId=Integer.parseInt(editorChiefIdStr);
		editorChiefService=(EditorChiefService) SpringUtil.getBean("editorChiefService");
		
		editorChief=editorChiefService.findById(editorChiefId);
		userService=(UserService) SpringUtil.getBean("userService");
		user=userService.findById(editorChief.getUserId());
		userExtService=(UserExtService) SpringUtil.getBean("userExtService");
		userExt=userExtService.findByUserId(editorChief.getUserId());
		
		return SUCCESS;
	}
	
	public String killChief(){
		
		int editorChiefId=Integer.parseInt(editorChiefIdStr);
		editorChiefService=(EditorChiefService) SpringUtil.getBean("editorChiefService");
		int id =editorChiefService.findById(editorChiefId).getUserId();
		
		boolean flag = editorChiefService.killChief(id);
		
		if(flag==true)
			return SUCCESS;
		
		return ERROR;
	}
	public String recoverChief(){
		int editorChiefId=Integer.parseInt(editorChiefIdStr);
		editorChiefService=(EditorChiefService) SpringUtil.getBean("editorChiefService");
		int id =editorChiefService.findById(editorChiefId).getUserId();
		boolean flag = editorChiefService.recoverChief(id);
		if(flag==true)
			return SUCCESS;
		return ERROR;
	}
		
	/**
	 * 主编黑名单列表
	 * @return
	 */
	public String  blackChiefList(){
		String url= "user/blackChiefList";
		editorChiefService=(EditorChiefService) SpringUtil.getBean("editorChiefService");
		pages = editorChiefService.blackChiefList((page-1)*10, url);
		return SUCCESS;
	}
	
	/*
	 * 更改主编状态
	 */
	public String updateStatus(){
		editorChiefService = (EditorChiefService) SpringUtil.getBean("editorChiefService");
		int editorChiefId = Integer.parseInt(editorChiefIdStr);
		editorChief.setEditorChiefId(editorChiefId);
		editorChiefService.updateStatus(editorChief);
		editorChief = editorChiefService.findById(editorChiefId);
		user = userService.findById(editorChief.getUserId());
		if("2".equals(editorChief.getStatus())) {
			user.setRoleId(4);
			userService.updateRole(user);
			//   发送通过信息  1  
			messageService.sendMessage(user, messageService, "1");
		}else {
			//  发送不通过信息 2
			messageService.sendMessage(user, messageService, "2");
		}
		return SUCCESS;
	}
	
	public String isAppliedChief() {
		int userId = super.getUserIdBySession();
		boolean result = editorChiefService.isApplied(userId);
		
		dataMap.clear();
		dataMap.put("result", result);
		
		return "success";
	}
	
	public Map<String, Object> getDataMap() {
		return dataMap;
	}
	public void setDataMap(Map<String, Object> dataMap) {
		this.dataMap = dataMap;
	}
	public EditorChiefService getEditorChiefService() {
		return editorChiefService;
	}
	@Resource(name="editorChiefService")
	public void setEditorChiefService(EditorChiefService editorChiefService) {
		this.editorChiefService = editorChiefService;
	}

	public EditorChief getEditorChief() {
		return editorChief;
	}

	public void setEditorChief(EditorChief editorChief) {
		this.editorChief = editorChief;
	}

	public File getBusinessImg() {
		return businessImg;
	}
	public void setBusinessImg(File businessImg) {
		this.businessImg = businessImg;
	}
	public String getBusinessImgFileName() {
		return businessImgFileName;
	}
	public void setBusinessImgFileName(String businessImgFileName) {
		this.businessImgFileName = businessImgFileName;
	}
	public String getBusinessImgContentType() {
		return businessImgContentType;
	}
	public void setBusinessImgContentType(String businessImgContentType) {
		this.businessImgContentType = businessImgContentType;
	}
	public File getCodeImg() {
		return codeImg;
	}
	public void setCodeImg(File codeImg) {
		this.codeImg = codeImg;
	}
	public String getCodeImgFileName() {
		return codeImgFileName;
	}
	public void setCodeImgFileName(String codeImgFileName) {
		this.codeImgFileName = codeImgFileName;
	}
	public String getCodeImgContentType() {
		return codeImgContentType;
	}
	public void setCodeImgContentType(String codeImgContentType) {
		this.codeImgContentType = codeImgContentType;
	}
	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}
	public String getEditorChiefIdStr() {
		return editorChiefIdStr;
	}
	public void setEditorChiefIdStr(String editorChiefIdStr) {
		this.editorChiefIdStr = editorChiefIdStr;
	}
	public UserService getUserService() {
		return userService;
	}
	@Resource(name="userService")
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public UserExtService getUserExtService() {
		return userExtService;
	}
	@Resource(name="userExtService")
	public void setUserExtService(UserExtService userExtService) {
		this.userExtService = userExtService;
	}
	public UserExt getUserExt() {
		return userExt;
	}
	public void setUserExt(UserExt userExt) {
		this.userExt = userExt;
	}
	public MessageService getMessageService() {
		return messageService;
	}
	@Resource(name="messageService")
	public void setMessageService(MessageService messageService) {
		this.messageService = messageService;
	}
	
}
